package 两数相加;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Solution {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        List<Integer> list =new ArrayList<>();
        int yu = 0;
        while(true){

            int l1Val = l1 == null ? 0 : l1.val;
            int l2Val = l2 == null ? 0 : l2.val;

            int sum = l1Val + l2Val + yu;

            yu = sum / 10;

            sum = sum % 10;

            list.add(sum);

            l1 = l1 != null ? l1.next == null ? null : l1.next : null;
            l2 = l2 != null ? l2.next == null ? null : l2.next : null;


            if(l1 == null && l2 == null  ){
                break;
            }
        }

        if(yu > 0)
            list.add(yu);




        ListNode currentNode = null;


        ListNode node = new ListNode(list.get(list.size() -1));

        for (int i =list.size() - 2;  i >= 0; i--) {

            int currentValue = list.get(i );
            ListNode tmp = new ListNode(currentValue,node);
            node = tmp;

        }


        return node;
    }



    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
